# -*- coding: utf-8 -*-
# @Time    : 2019/10/19 10:17
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : attr_desc.py

from datetime import date, datetime
import numbers


class IntField:
    """
    数据属性描述符，要实现__set__方法

    属性描述符
    实现下列三种方法之一即为属性描述符对象
    控制属性赋值
    """
    def __get__(self, instance, owner):
        return self.value

    def __set__(self, instance, value):
        if not isinstance(value, numbers.Integral):
            raise ValueError("int value need")
        if value < 0:
            raise ValueError("positive value need")
        self.value = value

    def __delete__(self, instance):
        pass


class NonDataIntField:
    """非数据属性描述符，只需要实现__get__方法"""
    def __get__(self, instance, owner):
        pass


class User:
    age = IntField()
    age_new = 1



# class User:
#     def __init__(self, name, email, birthday):
#         self.name = name
#         self.email = email
#         self.birthday = birthday
#         self._age = 0

    # def get_age(self):
    #     return datetime.now().year - self.birthday.year

    # @property  # 计算属性 ,将函数变为属性描述符
    # def age(self):  # 用处:可以放入时区转换逻辑
    #     return datetime.now().year - self.birthday.year
    #
    # @age.setter  # 将函数以属性赋值的方式进行赋值
    # def age(self, value):
    #     self._age = value


if __name__ == '__main__':
    user = User()
    user.age_new = 30
    user.age = 18  # 进入Intfield的value中没有进入到user的实例中
    print(user.__dict__)
    print(User.__dict__)
    print(getattr(user, "age"))
    # print(user.age)
    # print(user._age)  # 30
    # print(user.age)  # 32